package com.example.demo.controller;

import com.example.demo.common.ResultAjax;
import com.example.demo.common.SessionUtils;
import com.example.demo.model.Articleinfo;
import com.example.demo.model.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.REException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    //将
    @Autowired
    private ArticleService articleService;

    private static final int _DESC_LENGTH = 120;// 简介的长度

    /**
     * 得到当前用户文章的列表
     * @param request
     * @return
     */
    @RequestMapping("/mylist")
    public ResultAjax myList(HttpServletRequest request) {
        // 1.得到当前用户
        Userinfo userinfo = SessionUtils.getUser(request);
        // 用户不存在返回错误信息
        if (userinfo == null) {
            return ResultAjax.fail(-1,"未登录！");
        }
        //2.根据用户 id 查询此用户发表的所有文章
        List<Articleinfo> list = articleService.getListByUid(userinfo.getId());
        // 将文章变为简介
        if (list != null && list.size() > 0) {
            list.stream().parallel().forEach((articleinfo)->{
                     if (articleinfo.getContent().length() > 120) {
                         articleinfo.setContent(articleinfo.getContent().substring(0, _DESC_LENGTH));
                     }
            });
        }
        //3.返回给前端
        return ResultAjax.success(list);
    }

    /**
     * 删除文章操作
     * @param aid
     * @param request
     * @return
     */
    @RequestMapping("/del")
    public ResultAjax del(Integer aid,HttpServletRequest request){
        // 1.参数校验
        if(aid == null || aid <=0) {
            return ResultAjax.fail(-1,"参数错误!");
        }
        // 2.得到当前用户
        Userinfo userinfo = SessionUtils.getUser(request);
        if (userinfo == null) {
            return ResultAjax.fail(-1,"请先登录!");
        }
        // 3.判断文章归属
        int result = articleService.del(aid,userinfo.getId());
        // 4.将结果返回给前端
        return ResultAjax.success(result);
    }

    /**
     * 添加文章操作
     * @param articleinfo
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public ResultAjax add(Articleinfo articleinfo,HttpServletRequest request) {
        // 1.参数校验
        if(articleinfo == null || !StringUtils.hasLength(articleinfo.getTitle())
        || !StringUtils.hasLength(articleinfo.getContent())) {
            return ResultAjax.fail(-1,"非法参数!");
        }
        // 2.组装数据
        Userinfo userinfo = SessionUtils.getUser(request);
        if (userinfo == null) {
            return ResultAjax.fail(-1,"未登录请先登录!");
        }
        // 从 session 中获取当前登录的用户id传入给当前添加文章的uid
        articleinfo.setUid(userinfo.getId());
        // 3.将数据存入数据库
        int result = articleService.add(articleinfo);
        // 4.将结果返回给前端
        return ResultAjax.success(result);
    }
}
